package com.tbit.uqbike.object.business;

import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.object.pojo.AccountShortInfo;
import com.tbit.uqbike.object.pojo.MachineShortInfo;

import java.util.*;

/**
 * @author MyWin E-mail:335918956@qq.com
 * @version 1.0
 * @createTime 2020-01-02 19:18
 * 用户简要信息，包含了用户所有相关的权限信息（管理员身份、品牌ID、加盟商ID、区域代理商ID、运维管理员、运维人员）
 * 常规鉴权都通过此对象进行
 */
public class UserSchemaInfo {
    // 基本属性
    // 设备信息
    private List<MachineShortInfo> machines;
    // 账号信息
    private List<AccountShortInfo> accounts;
    // 设备扩展信息
    private Map<String, MachineShortInfo> codeMap = new HashMap<>();
    private Map<String, MachineShortInfo> mnoMap = new HashMap<>();
    private Map<Integer, MachineShortInfo> midMap = new HashMap<>();
    // 账号信息
    private Map<Integer, AccountShortInfo> accMap = new HashMap<>();
    // 管理员标志
    private Integer adminId;
    private AccountShortInfo admin;
    // 品牌
    private List<Integer> bIds = new LinkedList<>();
    private List<AccountShortInfo> bList = new LinkedList<>();
    private Map<Integer, AccountShortInfo> bMap = new HashMap<>();
    // 加盟商
    private List<Integer> fIds = new LinkedList<>();
    private List<AccountShortInfo> fList = new LinkedList<>();
    private Map<Integer, AccountShortInfo> fMap = new HashMap<>();
    // 运营区域
    private List<Integer> oIds = new LinkedList<>();
    private List<AccountShortInfo> oList = new LinkedList<>();
    private Map<Integer, AccountShortInfo> oMap = new HashMap<>();
    public UserSchemaInfo(List<MachineShortInfo> machines, List<AccountShortInfo> accounts) {
        this.machines = machines;
        this.accounts = accounts;
        // 完善扩展信息
        if (null != machines) {
            for (MachineShortInfo item : machines) {
                codeMap.put(item.getUserCode(), item);
                mnoMap.put(item.getMachineNO(), item);
                midMap.put(item.getMachineId(), item);
            }
        }
        if (null != accounts) {
            for (AccountShortInfo item : accounts) {
                accMap.put(item.getAccountId(), item);
            }
            for (AccountShortInfo item : accounts) {
                if (Objects.equals(item.getType(), UserConstant.USER_ADMIN)) {
                    adminId = item.getAccountId();
                    admin = item;
                } else if (Objects.equals(item.getType(), UserConstant.USER_BRAND)) {
                    bIds.add(item.getAccountId());
                    bList.add(item);
                    bMap.put(item.getAccountId(), item);
                } else if (Objects.equals(item.getType(), UserConstant.USER_FRANCHISEE)) {
                    AccountShortInfo p = accMap.get(item.getParentId());
                    if (null != p) {
                        p.fIds.add(item.getAccountId());
                        p.fList.add(item);
                        p.fMap.put(item.getAccountId(), item);
                    } else {
                        fIds.add(item.getAccountId());
                        fList.add(item);
                        fMap.put(item.getAccountId(), item);
                    }
                } else if (Objects.equals(item.getType(), UserConstant.USER_AGENT)) {
                    AccountShortInfo p = accMap.get(item.getParentId());
                    if (null != p) {
                        p.oIds.add(item.getAccountId());
                        p.oList.add(item);
                        p.oMap.put(item.getAccountId(), item);
                    } else {
                        oIds.add(item.getAccountId());
                        oList.add(item);
                        oMap.put(item.getAccountId(), item);
                    }
                }
            }
        }
    }

    // get

    public Map<String, MachineShortInfo> getCodeMap() {
        return codeMap;
    }

    public Map<String, MachineShortInfo> getMnoMap() {
        return mnoMap;
    }

    public Map<Integer, MachineShortInfo> getMidMap() {
        return midMap;
    }

    public Map<Integer, AccountShortInfo> getAccMap() {
        return accMap;
    }

    public Integer getAdminId() {
        return adminId;
    }

    public AccountShortInfo getAdmin() {
        return admin;
    }

    public List<Integer> getbIds() {
        return bIds;
    }

    public List<AccountShortInfo> getbList() {
        return bList;
    }

    public Map<Integer, AccountShortInfo> getbMap() {
        return bMap;
    }

    public List<Integer> getfIds() {
        return fIds;
    }

    public List<AccountShortInfo> getfList() {
        return fList;
    }

    public Map<Integer, AccountShortInfo> getfMap() {
        return fMap;
    }

    public List<Integer> getoIds() {
        return oIds;
    }

    public List<AccountShortInfo> getoList() {
        return oList;
    }

    public Map<Integer, AccountShortInfo> getoMap() {
        return oMap;
    }


    // 扩展方法

    /**
     * 是否是管理员
     *
     * @return
     */
    public boolean isAdmin() {
        return adminId != null && admin != null;
    }

    /**
     * 根据id获取角色对象，获取得到就是有权，获取不到就是无权操作
     *
     * @param accId
     * @return
     */
    public AccountShortInfo getAccById(Integer accId) {
        return accMap.get(accId);
    }

}
